package leetcode.array;

import java.util.Arrays;

/**
 * @author mazouri
 * @create 2021-05-02 18:53
 * <p>
 * https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
 */
public class MedianOfTwoSortedArrays {
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] nums3 = new int[nums1.length + nums2.length];
        System.arraycopy(nums1, 0, nums3, 0, nums1.length);
        System.arraycopy(nums2, 0, nums3, nums1.length, nums2.length);
        Arrays.sort(nums3);

        int flag = nums3.length % 2;
        if (flag == 0) {
            int right = nums3.length / 2;
            return (nums3[right - 1] + nums3[right]) / 2.0;
        } else {
            return nums3[nums3.length / 2];
        }
    }

    public static void main(String[] args) {
        int[] num1 = {1,2};
        int[] num2 = {3,4};
        System.out.println(findMedianSortedArrays(num1, num2));
    }
}
